001 /*
002 * Copyright 2006 Stephen McConnell.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package net.dpml.http;
017
018 import java.util.logging.Level;
019 import java.util.logging.Logger;
020
021 /**
022 * Wrapper to redirect Jetty logging to standard JVM logging.
023 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
024 * @version 0.0.1
025 */
026 public class LogAdapter implements org.mortbay.log.Logger
027 {
028 private final Logger m_logger;
029
030 /**
031 * Creation of a new log adapter.
032 */
033 public LogAdapter()
034 {
035 this( Logger.getLogger( "org.mortbay" ) );
036 }
037
038 /**
039 * Creation of a new log adapter.
040 * @param logger the underlying logging channel
041 */
042 LogAdapter( Logger logger )
043 {
044 m_logger = logger;
045 }
046
047 /**
048 * Get the debug enabled status.
049 * @return true if debug is enabled
050 */
051 public boolean isDebugEnabled()
052 {
053 return m_logger.isLoggable( Level.FINE );
054 }
055
056 /**
057 * Set the debug enabled status.
058 * @param flag true if debug is enabled
059 */
060 public void setDebugEnabled( boolean flag )
061 {
062 }
063
064 /**
065 * Publish an info level log message.
066 * @param msg the message
067 * @param arg0 an intial argument
068 * @param arg1 a subsequent argument
069 */
070 public void info( String msg, Object arg0, Object arg1 )
071 {
072 if( m_logger.isLoggable( Level.INFO ) )
073 {
074 String message = format( msg, arg0, arg1 );
075 m_logger.info( message );
076 }
077 }
078
079 /**
080 * Publish an debug level log message.
081 * @param message the message
082 * @param cause an exception
083 */
084 public void debug( String message, Throwable cause )
085 {
086 if( isDebugEnabled() )
087 {
088 m_logger.log( Level.FINE, message, cause );
089 }
090 }
091
092 /**
093 * Publish an debug level log message.
094 * @param msg the message
095 * @param arg0 an intial argument
096 * @param arg1 a subsequent argument
097 */
098 public void debug( String msg, Object arg0, Object arg1 )
099 {
100 if( isDebugEnabled() )
101 {
102 String message = format( msg, arg0, arg1 );
103 m_logger.log( Level.FINE, message );
104 }
105 }
106
107 /**
108 * Publish an warning level log message.
109 * @param msg the message
110 * @param arg0 an intial argument
111 * @param arg1 a subsequent argument
112 */
113 public void warn( String msg, Object arg0, Object arg1 )
114 {
115 if( m_logger.isLoggable( Level.WARNING ) )
116 {
117 String message = format( msg, arg0, arg1 );
118 m_logger.log( Level.WARNING, message );
119 }
120 }
121
122 /**
123 * Publish an warning level log message.
124 * @param message the message
125 * @param error an exception
126 */
127 public void warn( String message, Throwable error )
128 {
129 if( m_logger.isLoggable( Level.WARNING ) )
130 {
131 m_logger.log( Level.WARNING, message, error );
132 }
133 }
134
135 private String format( String msg, Object arg0, Object arg1 )
136 {
137 int i0 = msg.indexOf( "{}" );
138
139 int i1 = 0;
140 if( i0 < 0 )
141 {
142 i1 = -1;
143 }
144 else
145 {
146 i1 = msg.indexOf( "{}" , i0 + 2 );
147 }
148
149 if( ( arg1 != null ) && ( i1 >= 0 ) )
150 {
151 msg =
152 msg.substring( 0, i1 )
153 + arg1
154 + msg.substring( i1 + 2 );
155 }
156 if( ( arg0 != null ) && ( i0 >= 0 ) )
157 {
158 msg =
159 msg.substring( 0, i0 )
160 + arg0
161 + msg.substring( i0 + 2 );
162 }
163 return msg;
164 }
165
166 /**
167 * Create a logger matching the supplied category.
168 * @param category the category name
169 * @return the logging channel
170 */
171 public org.mortbay.log.Logger getLogger( String category )
172 {
173 if( ( null == category ) || "org.mortbay".equals( category ) )
174 {
175 return this;
176 }
177 else
178 {
179 String name = m_logger.getName();
180 String path = trim( name + "." + category );
181 return new LogAdapter( Logger.getLogger( path ) );
182 }
183 }
184
185 private String trim( String path )
186 {
187 if( path.startsWith( "." ) )
188 {
189 return trim( path.substring( 1 ) );
190 }
191 else if( ".".equals( path ) )
192 {
193 return "";
194 }
195 else
196 {
197 return path;
198 }
199 }
200
201 /**
202 * Return a string representation of this logger.
203 * @return the string value
204 */
205 public String toString()
206 {
207 return "java.util.logging.Logger/" + m_logger.getName();
208 }
209
210 }
211